package com.qiwenshare.file.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.api.RoleService;
import com.qiwenshare.file.domain.SysRole;
import com.qiwenshare.file.domain.user.Role;
import com.qiwenshare.file.dto.role.BatchModifyRoleFilePermissionDto;
import com.qiwenshare.file.mapper.RoleFilePermissionMapper;
import com.qiwenshare.file.domain.RoleFilePermission;
import com.qiwenshare.file.api.RoleFilePermissionService;
import com.qiwenshare.file.vo.role.RoleFilePermissionVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色文件操作权限(RoleFilePermission)表服务实现类
 *
 * @author MJ
 * @since 2023-10-11 15:51:36
 */
@Service("roleFilePermissionService")
public class RoleFilePermissionServiceImpl extends ServiceImpl<RoleFilePermissionMapper, RoleFilePermission> implements RoleFilePermissionService {

    @Autowired
    RoleService roleService;

    @Autowired
    RoleFilePermissionMapper roleFilePermissionMapper;

    @Override
    public List<RoleFilePermission> queryList(String userFileId) {
        List<RoleFilePermission> oldPermission = this.list(new QueryWrapper<>(RoleFilePermission.builder()
                .userFileId(userFileId)
                .build()));
        List<SysRole> roleList = roleService.queryAllRoleList();
        List<RoleFilePermission> newPermission = new ArrayList<>();
        for (SysRole role: roleList) {
            boolean isExist = false;
            for (RoleFilePermission roleFilePermission: oldPermission) {
                if (role.getRoleId().equals(roleFilePermission.getRoleId())) {
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                newPermission.add(RoleFilePermission.builder()
                                .id(IdUtil.getSnowflakeNextIdStr())
                                .roleId(role.getRoleId())
                                .userFileId(userFileId)
                                .createUserId(SessionUtil.getSession().getUserId())
                                .createTime(DateUtil.getCurrentTime())
                        .build());
            }
        }
        if (CollectionUtils.isNotEmpty(newPermission)) {
            this.saveBatch(newPermission);
            oldPermission = this.list(new QueryWrapper<>(RoleFilePermission.builder()
                    .userFileId(userFileId)
                    .build()));
        }
        return oldPermission;
    }

    @Override
    public List<RoleFilePermissionVo> queryListByUser() {
        return roleFilePermissionMapper.queryListByUser(SessionUtil.getUserId());
    }

    @Override
    public Boolean modifyBatch(BatchModifyRoleFilePermissionDto batchModifyRoleFilePermissionDto) {
        return this.updateBatchById(batchModifyRoleFilePermissionDto.getPermissionList());
    }
}

